package com.travel.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.travel.commons.utils.Constant;
import com.travel.commons.utils.R;
import com.travel.modules.asset.entity.*;
import com.travel.modules.asset.service.*;
import com.travel.modules.sys.entity.SysMsg;
import com.travel.modules.sys.service.SysMsgService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sys/msg")
public class SysMsgController {
    @Resource
    private SysMsgService sysMsgService;

    @Resource
    private ScenicAreaService scenicAreaService;

    @Resource
    private HotelService hotelService;

    @Resource
    private RouteService routeService;

    @Resource
    private AdvertiseService advertiseService;

    @Resource
    private GoodsService goodsService;

    /**
     * 消息列表
     */
    @RequestMapping("/list")
    public R list() throws ClassNotFoundException {
        List<SysMsg> list = sysMsgService.getAllMsg();
        return R.ok().put("page", list);
    }

    /**
     * 消息详情
     * @param form id - 消息id
     */
    @RequestMapping("/info")
    public R info(@RequestBody SysMsg form) {
        Integer id = form.getId();
        if (id == null) {
            return R.error("缺少id");
        }

        SysMsg sysMsg = sysMsgService.getById(form.getId());
        if (sysMsg == null) {
            return R.error("消息飞走了");
        }
        return R.ok().put("page", sysMsg);
    }

    /**
     * 编辑消息
     */
    @RequestMapping("/edit")
    public R edit() {
        return R.ok();
    }

    /**
     * 审核通过
     * @param form id
     */
    @RequestMapping("/pass")
    public R ok(@RequestBody SysMsg form) {
        Integer id = form.getId();
        if (id == null) {
            return R.error("缺少id");
        }

        SysMsg sysMsg = sysMsgService.getById(id);
        if (sysMsg == null) {
            return R.error("消息飞走了");
        }
        Object obj = sysMsgService.pass(sysMsg.getTargetId(), sysMsg.getTableName(), sysMsg);
        if (obj instanceof R) {
            return (R) obj;
        }
        return R.ok();
    }

    /**
     * 审核不通过
     * @param form id
     */
    @RequestMapping("/fail")
    public R no(@RequestBody SysMsg form) {
        Integer id = form.getId();
        if (id == null) {
            return R.error("缺少id");
        }

        SysMsg sysMsg = sysMsgService.getById(id);
        if (sysMsg == null) {
            return R.error("没有该消息");
        }
        Object obj = sysMsgService.fail(sysMsg.getTargetId(), sysMsg.getTableName(), sysMsg);
        if (obj instanceof R) {
            return (R) obj;
        }
        return R.ok();
    }

    @RequestMapping("/scenic")
    public R scenicMsg() {
        Map<String, Object> param = new HashMap<>();
        param.put("status", 0);
        List<ScenicArea> list = scenicAreaService.getAllScenicArea(param).stream().sorted(Comparator.comparing(ScenicArea ::getStatus)).collect(Collectors.toList());
        return R.ok().put("page", list);
    }

    @RequestMapping("/scenic/info")
    public R scenicMsgInfo(@RequestBody ScenicArea form) {
        String areaId = form.getAreaId();
        if (StringUtils.isBlank(areaId)){
            return R.error("缺少id");
        }
        return R.ok().put("page", scenicAreaService.getOneScenicArea(areaId));
    }

    @RequestMapping("/scenic/status")
    public R scenicMsgStatus(@RequestBody ScenicArea form) {
        String areaId = form.getAreaId();
        if (StringUtils.isBlank(areaId)) {
            return R.error("缺少id");
        }
        Integer status = form.getStatus();
        if (status == null) {
            return R.error("缺少参数");
        }
        if (status != -1 && status != 0 && status != 1 && status != 2) {
            return R.error("参数不合法");
        }
        ScenicArea old = scenicAreaService.getOne(new QueryWrapper<ScenicArea>().eq("area_id", areaId));
        old.setStatus(status);
        scenicAreaService.updateById(old);
        if (status == 1) {
            goodsService.save(Goods.builder().goodsId(form.getAreaId()).tableName(Constant.T_SCENIC_AREA).build());
        }
        return R.ok();
    }

    @RequestMapping("/hotel")
    public R hotelMsg() {
        Map<String, Object> param = new HashMap<>();
        param.put("status", 0);
        return R.ok().put("page", hotelService.getAllHotel(param).stream().sorted(Comparator.comparing(Hotel ::getStatus)).collect(Collectors.toList()));
    }

    @RequestMapping("/hotel/info")
    public R hotelMsgInfo(@RequestBody Hotel form) {
        String hotelId = form.getHotelId();
        if (StringUtils.isBlank(hotelId)) {
            return R.error("缺少id");
        }
        return R.ok().put("page", hotelService.getOneHotel(hotelId));
    }

    @RequestMapping("/hotel/status")
    public R hotelMsgStatus(@RequestBody Hotel form) {
        String hotelId = form.getHotelId();
        if (StringUtils.isBlank(hotelId)) {
            return R.error("缺少id");
        }
        Integer status = form.getStatus();
        if (status == null) {
            return R.error("缺少参数");
        }
        if (status != -1 && status != 0 && status != 1 && status != 2) {
            return R.error("参数不合法");
        }
        Hotel old = hotelService.getOne(new QueryWrapper<Hotel>().eq("hotel_id", hotelId));
        old.setStatus(status);
        hotelService.updateById(old);
        if (status == 1) {
            goodsService.save(Goods.builder().goodsId(form.getHotelId()).tableName(Constant.T_HOTEL).build());
        }
        return R.ok();
    }

    @RequestMapping("/route")
    public R routeMsg() {
        Map<String, Object> param = new HashMap<>();
        param.put("status", 0);
        return R.ok().put("page", routeService.getAllRoute(param).stream().sorted(Comparator.comparing(Route ::getStatus)).collect(Collectors.toList()));
    }

    @RequestMapping("/route/info")
    public R hotelMsgInfo(@RequestBody Route form) {
        String goodsId = form.getGoodsId();
        if (StringUtils.isBlank(goodsId)){
            return R.error("缺少id");
        }
        return R.ok().put("page", routeService.getOneRoute(goodsId));
    }

    @RequestMapping("/route/status")
    public R routeMsgStatus(@RequestBody Route form) {
        String goodsId = form.getGoodsId();
        if (StringUtils.isBlank(goodsId)) {
            return R.error("缺少id");
        }
        Integer status = form.getStatus();
        if (status == null) {
            return R.error("缺少参数");
        }
        if (status != -1 && status != 0 && status != 1 && status != 2) {
            return R.error("参数不合法");
        }
        Route old = routeService.getOne(new QueryWrapper<Route>().eq("goods_id", goodsId));
        old.setStatus(status);
        routeService.updateById(old);
        if (status == 1) {
            goodsService.save(Goods.builder().goodsId(form.getGoodsId()).tableName(Constant.T_ROUTE).build());
        }
        return R.ok();
    }

    @RequestMapping("/advertise")
    public R advertiseMsg() {
        Map<String, Object> param = new HashMap<>();
        param.put("status", 0);
        return R.ok().put("page", advertiseService.getAllAdvertise(param).stream().sorted(Comparator.comparing(Advertise ::getStatus)).collect(Collectors.toList()));
    }

    @RequestMapping("/advertise/info")
    public R advertiseMsgInfo(@RequestBody Advertise form) {
        String adId = form.getAdId();
        if (StringUtils.isBlank(adId)){
            return R.error("缺少id");
        }
        return R.ok().put("page", advertiseService.getOneAdvertise(adId));
    }

    @RequestMapping("/advertise/status")
    public R advertiseMsgStatus(@RequestBody Advertise form) {
        String adId = form.getAdId();
        if (StringUtils.isBlank(adId)) {
            return R.error("缺少id");
        }
        Integer status = form.getStatus();
        if (status == null) {
            return R.error("缺少参数");
        }
        if (status != -1 && status != 0 && status != 1 && status != 2) {
            return R.error("参数不合法");
        }
        Advertise old = advertiseService.getOne(new QueryWrapper<Advertise>().eq("ad_id", adId));
        old.setStatus(status);
        advertiseService.updateById(old);
        return R.ok();
    }
}
